﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.Xml.Serialization;
using Zipun.VkOnlineMusic.Api.Domain;

namespace Zipun.VkOnlineMusic.Api
{
    public class AudioManager
    {
        private readonly VkToken _vkToken;

        public AudioManager(VkToken vkToken)
        {
            _vkToken = vkToken;
        }

        private WebResponse requestApiMethod(string methodName, string parameters = null)
        {
            if (parameters == null)
                parameters = string.Empty;
            var requestString = string.Format("{0}{1}?{2}{3}={4}", VkApiConstant.MethodsUrl,
                                              methodName, parameters,
                                              VkApiConstant.Parameter.AccessToken, _vkToken.AccessToken);
            var request = WebRequest.Create(requestString);
            return request.GetResponse();
        }

        public List<VkAudio> GetList()
        {
            return getAudioList(VkApiConstant.Metod.Audio.Get);
        }

        public List<VkAudio> GetSuggestions()
        {
            return getAudioList(VkApiConstant.Metod.Audio.GetRecomendations);
        }

        private List<VkAudio> getAudioList(string method)
        {
            var responce = requestApiMethod(method);
            var reader = new XmlTextReader(responce.ResponseUri.AbsoluteUri);
            var xs = new XmlSerializer(typeof(VkAudioList));
            VkAudioList val;
            try
            {
                val = xs.Deserialize(reader) as VkAudioList;
            }
            finally
            {
                responce.Close();
            }
            if (val == null)
                throw new NotImplementedException("need normail exception here");
            return val.Items;
        }

        public bool Add(uint audioId, uint ownerId)
        {
            var parameters = string.Format("{0}={1}&{2}={3}&",
                                           VkApiConstant.Parameter.AudioId, audioId,
                                           VkApiConstant.Parameter.OwnerId, ownerId);
            var responce = requestApiMethod(VkApiConstant.Metod.Audio.Add, parameters);
            var xmlSream = responce.GetResponseStream();
            if (xmlSream == null)
                return false;
            var doc = new XmlDocument();
            doc.Load(xmlSream);
            responce.Close();
            var addedIdNode = doc.SelectSingleNode("/response");
            return addedIdNode != null;
        }


        public bool Delete(uint audioId, uint ownerId)
        {
            var parameters = string.Format("{0}={1}&{2}={3}&",
                                           VkApiConstant.Parameter.AudioId, audioId,
                                           VkApiConstant.Parameter.OwnerId, ownerId);
            var response = requestApiMethod(VkApiConstant.Metod.Audio.Delete, parameters);
            var xmlStream = response.GetResponseStream();
            if (xmlStream == null)
                return false;
            var doc = new XmlDocument();
            doc.Load(xmlStream);
            response.Close();
            var responseNode = doc.SelectSingleNode("/response");
            return responseNode != null;
        }
    }
}